package com.lj.infoisland.controller;

import com.lj.infoisland.common.convention.result.Result;
import com.lj.infoisland.common.convention.result.Results;
import com.lj.infoisland.service.MinioService;
import com.lj.infoisland.util.UniqueRandomStringGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Objects;

@RestController
public class MinioController {

    @Value("${minio.FILE_PREFIX}")
    private String FILE_PREFIX;
    @Resource
    private MinioService minioService;

    // 上传md富文本编辑器的图片
    @PostMapping("/uploadMdImage")
    public String uploadMdImage(@RequestParam("file") MultipartFile file) throws Exception {
        int dotIndex = Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf('.');
        String suffix = file.getOriginalFilename().substring(dotIndex);
        String filename = UniqueRandomStringGenerator.generateUniqueString() + suffix;
        Boolean put = minioService.put(file.getInputStream(), file.getContentType(), filename);
        return FILE_PREFIX + filename;
    }

    // 上传图片
    @PostMapping("/uploadImage")
    public Result<String> uploadImage(@RequestParam("file") MultipartFile file) throws Exception {
        int dotIndex = Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf('.');
        String suffix = file.getOriginalFilename().substring(dotIndex);
        String filename = UniqueRandomStringGenerator.generateUniqueString() + suffix;
        Boolean put = minioService.put(file.getInputStream(), file.getContentType(), filename);
        return Results.success(FILE_PREFIX + filename);
    }

    // 上传图片
    @PostMapping("/uploadCoverImage")
    public Result<String> uploadCoverImage(@RequestParam("file") MultipartFile file) throws Exception {
        int dotIndex = Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf('.');
        String suffix = file.getOriginalFilename().substring(dotIndex);
        String filename = UniqueRandomStringGenerator.generateUniqueString() + suffix;
        Boolean put = minioService.put(file.getInputStream(), file.getContentType(), filename);
        return Results.success(filename);
    }


    // 上传文件
    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) throws Exception {
        Boolean put = minioService.put(file.getInputStream(), file.getContentType(), file.getOriginalFilename());
        return file.getOriginalFilename();
    }

//    // 下载文件
//    @GetMapping("/download")
//    public void download(@RequestParam("fileName")String fileName, HttpServletResponse response) {
//        this.minioConfig.download(fileName,response);
//    }

//    // 删除一个对象
//    @PostMapping("/removeObject")
//    public boolean removeObject(String bucketName, String objectName) throws Exception {
//        return this.minioConfig.removeObject(bucketName, objectName);
//    }

    // 文件访问路径
    @PostMapping("/getObjectUrl")
    public String getObjectUrl(String objectName) {
        return minioService.getDownloadUrl(objectName);
    }
}
 
 
 